package com.ndhutu.ee.util.map.baidu;

import com.alibaba.fastjson.JSONObject;
import com.ndhutu.ee.util.PageUtil;
import com.ndhutu.ee.util.http.OKHttpUtil;
import sun.applet.Main;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Shane
 * @Date 2020/3/21 15:10
 */
public class MapUtil {
    /**
     * 获取IP地址的位置信息
     * @Author Shane
     * @Date 2020/3/21 15:26
     * @param ip    ip地址
     * @return
     *
     * {
     *     address: "CN|北京|北京|None|CHINANET|1|None",    #详细地址信息
     *     content:    #结构信息
     *     {
     *         address: "北京市",    #简要地址信息
     *         address_detail:    #结构化地址信息
     *         {
     *             city: "北京市",    #城市
     *             city_code: 131,    #百度城市代码
     *             district: "",    #区县
     *             province: "北京市",    #省份
     *         },
     *         point:    #当前城市中心点
     *         {
     *             x: "116.39564504",    #当前城市中心点经度
     *             y: "39.92998578"    #当前城市中心点纬度
     *         }
     *     },
     *     status: 0    #结果状态返回码
     * }
     */
    public static Map<String, Object> getIpAddr(String ip, MapConfig config) throws Exception{
        Map<String, String> params = new HashMap<String, String>();
        params.put("ip", ip);
        params.put("ak", config.getAk());
        params.put("coor", "bd09ll");

        String result = OKHttpUtil.get("http://api.map.baidu.com/location/ip", params);

        return JSONObject.parseObject(result, Map.class);
    }

    /**
     * 逆地理编码
     * @Author Shane
     * @Date 2020/4/1 22:45
     * @param
     * @return
     */
    public static Map<String, Object> geocoding(String latlng, MapConfig config) throws Exception{
        Map<String, String> params = new HashMap<String, String>();
        params.put("location", latlng);
        params.put("ak", config.getAk());
        params.put("output", "json");

        String resultJson = OKHttpUtil.get("http://api.map.baidu.com/reverse_geocoding/v3/", params);
//        System.out.println(resultJson);
        Map<String, Object> resultMap = JSONObject.parseObject(resultJson, Map.class);
        if(Integer.parseInt(resultMap.get("status")+"") == 0){
            return (Map<String, Object>)resultMap.get("result");
        }

        return null;
    }

    /**
     * 地点输入提示
     * @param region    城市或城市编码citycode
     * @param location  地址，如40.047857537164,116.31353434477
     * @param query 关键字
     * @return
     */
    public static List<Map<String, Object>> suggestion(String region, String location, String query, MapConfig config) throws Exception{
        Map<String, String> params = new HashMap<>();
        params.put("query", query);
        params.put("region", region);
        params.put("city_limit", "true");
        params.put("location", location);
        params.put("output", "json");
        params.put("ak", config.getAk());

        String result = OKHttpUtil.get("http://api.map.baidu.com/place/v2/suggestion", params);
        Map map = JSONObject.parseObject(result, Map.class);
        if(Integer.parseInt(map.get("status")+"") == 0){
            return (List<Map<String, Object>>) map.get("result");
        }

        return new ArrayList<>();
    }

    public static void main(String[] args) throws Exception {
        MapConfig mapConfig = new MapConfig() {
            public String getAk() {
                return "uNMNRDEKbIuac1edGfbGtLO4qYCAhI3q";
            }
        };

        Map<String, Object> map = getIpAddr("10.112.150.18", mapConfig);
        System.out.println(JSONObject.toJSONString(map));

        Map<String, Object> content = (Map)map.get("content");
        String city = ((Map)content.get("address_detail")).get("city")+"";
        String lng = ((Map)content.get("point")).get("x")+"";
        String lat = ((Map)content.get("point")).get("y")+"";
        System.out.println(lat+","+lng);
    }
}
